﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WallpaperControl
{
    public partial class ConditionForm : Form
    {
        /// <summary>
        /// The condition if the condition type is set to time.
        /// </summary>
        public static System.DateTime TimeCondition;
        /// <summary>
        /// The condition if the condition type is set to interval.
        /// </summary>
        public static string mInterval;
        public static int IntervalIndex;
        /// <summary>
        /// The type of condition. 1 = time, 2 = interval.
        /// </summary>
        public static int ConditionType;
        
        public ConditionForm()
        {
            InitializeComponent();
            dateTimePicker1.Format = DateTimePickerFormat.Time;
            dateTimePicker1.ShowUpDown = true;
            LoadConditionFile();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            ConditionForm.ActiveForm.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            //TimeCondition = dateTimePicker1.Value;
            SaveCondition();
            ConditionForm.ActiveForm.Close();
        }

        /// <summary>
        /// Loads the 'condition.xml' which contains the condition set by the user.
        /// </summary>
        private void LoadConditionFile()
        {
            System.IO.FileStream LoadFileStream = new System.IO.FileStream("condition.xml", System.IO.FileMode.OpenOrCreate);
            if (radioButtonTime.Checked)
            {
                System.Xml.Serialization.XmlSerializer XMLSerializer = new System.Xml.Serialization.XmlSerializer(typeof(DateTime));
                dateTimePicker1.Value = (DateTime)XMLSerializer.Deserialize(LoadFileStream);
            }
            if (radioButtonInterval.Checked)
            {
                mInterval = comboBox1.SelectedValue.ToString();
            }
            LoadFileStream.Close();
        }

        /// <summary>
        /// Saves the 'condition.xml' which contains the condition set by the user.
        /// </summary>
        private void CreateConditionFile()
        {
            System.IO.FileStream SaveFileStream = new System.IO.FileStream("condition.xml", System.IO.FileMode.Create);
            if (ConditionType == 1)
            {
                System.Xml.Serialization.XmlSerializer XMLSerializer = new System.Xml.Serialization.XmlSerializer(typeof(DateTime));
                XMLSerializer.Serialize(SaveFileStream, TimeCondition);
            }
            else if (ConditionType == 2)
            {
                System.Xml.Serialization.XmlSerializer XMLSerializer = new System.Xml.Serialization.XmlSerializer(typeof(int));
                XMLSerializer.Serialize(SaveFileStream, IntervalIndex);
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Fehler beim Erstellen der Datei 'condition.xml': Ihre Eingabe war nicht valide und konnte nicht verarbeitet werden.", "Fehler", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            SaveFileStream.Close();
        }

        /// <summary>
        /// Saves the 'conditiontype.xml' which contains the type of condition.
        /// </summary>
        private void CreateConditionTypeFile()
        {
            System.IO.FileStream FileStream = new System.IO.FileStream("conditiontype.xml", System.IO.FileMode.Create);
            System.Xml.Serialization.XmlSerializer XMLSerializer = new System.Xml.Serialization.XmlSerializer(typeof(int));
            XMLSerializer.Serialize(FileStream, ConditionType);
            FileStream.Close();
        }

        /// <summary>
        /// Saves the condition to a variable.
        /// </summary>
        private void SaveCondition()
        {
            if (radioButtonTime.Checked)
            {
                TimeCondition = dateTimePicker1.Value;
                ConditionType = 1;
            }
            else if (radioButtonInterval.Checked)
            {
                IntervalIndex = comboBox1.SelectedIndex;
                ConditionType = 2;
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Bitte wählen Sie aus, ob die Bedingung ein Zeitpunkt oder ein Intervall sein soll.", "Fehler - Eingabe fehlerhaft", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            CreateConditionTypeFile();
            CreateConditionFile();
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            IntervalIndex = comboBox1.SelectedIndex;
        }
    }
}
